package com.shanshan.system.filter;


import com.alibaba.fastjson.JSON;
import com.shanshan.system.vo.Result;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/01/04 13:13
 * @Description
 */
@Component
public class MySecuityFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 1.获取请求的请求方式
        String method = request.getMethod();
        String path = request.getRequestURI();
        String post = "POST", login = "/login",captchaImage="/captchaImage",image="/upload/sure",
                notice="/notice/list",news1="/system/news/newkind1",news2="/system/news/newkind2",
                news3="/system/news/newkind1ById",news4="/system/news/newkind2ById",
                branch="/system/branch/list",branchId="/system/branch/listId",
                adminBranchId="/system/admin/branchId",addRecord="/system/record/addRecord",
        adminId="/system/admin/selectId",countName="/system/admin/accountName",
        updateUsed="/system/meetingClass/updateUsed",echarts = "/echarts/record",
                getBranch="/echarts/getBranch",record="/system/record/list",excel="/excel/download";
        // 2.如果该请求为login请求方式直接放行
        if(post.equals(method) && login.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        // 放行获取验证码的路由信息
        if(captchaImage.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(excel.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(record.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(getBranch.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }

        // 放行获取验证码的路由信息
        if(echarts.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }

        //山传照片
        if(notice.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        //资源访问

        if(updateUsed.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(addRecord.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(countName.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(adminId.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(branchId.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(adminBranchId.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(branch.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(news1.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(news2.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(news3.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(news4.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }
        if(image.equals(path)){
            filterChain.doFilter(request,response);
            return;
        }

        // 3.不是登录请求方式获取该头信息中的token
        String token = request.getHeader("token");

        // 4.判断token是否正确
        if(token != null && com.shanshan.system.util.JwtUtil.verifyToken(token)) {
            // 5.把当前的用户和该用户具有的权限放入SecurityContext对象中。传入安全框架中的过滤器
            Map<String, Object> jwt = com.shanshan.system.util.JwtUtil.decodeJWT(token);
            String username = (String) jwt.get("username");
            List<String> permission = (List<String>) jwt.get("permission");

            Collection<GrantedAuthority> authorities =
                    permission.stream()
                            .map( SimpleGrantedAuthority::new)
                            .collect(Collectors.toList());

            UsernamePasswordAuthenticationToken passwordAuthenticationToken =
                    new UsernamePasswordAuthenticationToken(username,null,authorities);

            SecurityContextHolder.getContext().setAuthentication(passwordAuthenticationToken);
            filterChain.doFilter(request,response);

        }else {
            response.setContentType("json/application;charset=utf-8");
            PrintWriter writer = response.getWriter();
            Result result= new Result(4001,"未登录");
            String jsonString = JSON.toJSONString(result);
            writer.print(jsonString);
            writer.flush();
            writer.close();
        }
    }
}
